package com.wxm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xm.dao.MessageDaoImpl;
import com.xm.entity.Message;
import com.xm.dao.BannerDaoImpl;
import com.xm.dao.ComdeDaoImp;
import com.xm.dao.HomeDaoImpl;
import com.xm.dao.ImageDaoImpl;
import com.xm.dao.LianXiDaoImpl;
import com.xm.dao.NewsDaoImpl;
import com.xm.dao.ProductDaoImpl;
import com.xm.dao.ProtypeDaoImpl;
import com.xm.entity.Banners;
import com.xm.entity.Comdetail;
import com.xm.entity.Images;
import com.xm.entity.LianXi;
import com.xm.entity.News;
import com.xm.entity.Product;
import com.xm.entity.producttype;
import com.xm.uti.Result;

/**
 * 首页方法的调用
 * 
 * @author 吴雪曼
 *
 */
@WebServlet("/HomeServlet")
public class HomeServlet extends BaseServlet {

	/**
	 * 地址http://localhost:8080/javaweb-Furniture/HomeServlet?method=toIndex
	 */
	public void toIndex(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 轮播
		BannerDaoImpl banDao = new BannerDaoImpl();
		ArrayList<Banners> ban = banDao.getBanList();

		// 单个公司信息
		ComdeDaoImp dao = new ComdeDaoImp();
		Comdetail com = dao.getCom();

		// 推荐产品列表
		ProductDaoImpl tuiDao = new ProductDaoImpl();
		ArrayList<Product> arrayList = tuiDao.getShouList();

		// 推荐新闻里列表
		NewsDaoImpl newDao = new NewsDaoImpl();
		ArrayList<News> newList = newDao.getListLimit4();
		// 单个联系信息
		LianXiDaoImpl dao3 = new LianXiDaoImpl();
		LianXi com2 = dao3.getCom().get(0);

		HttpSession session = req.getSession();

		// 存储作用域
		req.setAttribute("ban", ban);
		req.setAttribute("arrayList", arrayList);
		req.setAttribute("newList", newList);
		
		session.setAttribute("com2", com2);
		session.setAttribute("com", com);
		// 跳转页面（请求转发）

		req.getRequestDispatcher("index.jsp").forward(req, resp);
	}

	/**
	 * 根据产品类外键获取产品列表 ,当点击超链接时调用
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProByIype(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 接收客户端的参数(数据类型的字符串)
		String protype = req.getParameter("protype");
		// 讲字符串转换，并判断不为空
		int typProInt = 0;
		if (protype != null && !protype.equals("")) {
			typProInt = Integer.valueOf(protype);
		}

		// 调用dao方法
		// 根据id查询,产品列表
		ProductDaoImpl protypes = new ProductDaoImpl();
		ArrayList<Product> selected = protypes.getListByProType(typProInt);
		// 产品类型列表
		ProtypeDaoImpl Protype = new ProtypeDaoImpl();
		ArrayList<producttype> allProtype = Protype.getAllProtype();

		// 存储在作用域
		req.setAttribute("selected", selected);// 根据产品列表id查询
		req.setAttribute("allProtype", allProtype);// 产品列表

		// 跳转页面(请求转发)
		req.getRequestDispatcher("productlist.jsp").forward(req, resp);
	}

	/**
	 * 留言板的方法
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void toMessage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 接收客户端的参数(数据类型的字符串)
		String name = req.getParameter("name");
		String tel = req.getParameter("tel");
		String email = req.getParameter("email");
		String content = req.getParameter("content");
		System.out.println("请求的参数是" + name + tel + email + content);
		// 调用dao方法
		MessageDaoImpl mess = new MessageDaoImpl();
		boolean res = mess.insertMessage(new Message(0, name, tel, email, content, null, content, null, content));
		// 判断结果，实现跳转
		if (res == true) {// 添加成功
			req.getRequestDispatcher("message.jsp").forward(req, resp);
		} else {// 添加失败
			req.setAttribute("res", "添加失败");
			req.getRequestDispatcher("message.jsp").forward(req, resp);
		}

	}
}